package com.jas.logic;

//@author A0098803W
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.jas.common.CommandFormatException;
import com.jas.common.DateFormatException;
import com.jas.common.DeadlineEvent;
import com.jas.common.Event;
import com.jas.common.EventDateException;
import com.jas.common.FloatingEvent;
import com.jas.common.Language;
import com.jas.common.SearchException;
import com.jas.common.Utilities;
import com.jas.storage.StorageAPI;
import com.jas.storage.StorageException;
import com.jas.ui.DisplayControl;

//@author A0098803W
/**
 * Theis class tests the delete and search function
 * 
 */
public class DeleteSearchTest {

	@Before
	public void setUp() throws Exception {
		Language.loadLanguageFile();
	}

	@After
	public void tearDown() throws Exception {
	}

	//@author A0098803W
	/**
	 * This method tests on the search and delete all events function.
	 * 
	 * @throws DateFormatException
	 * @throws EventDateException 
	 * @throws SearchException 
	 * @throws EventIdException 
	 * @throws StorageException 
	 * @throws CommandFormatException 
	 */
	@Test
	public void testSearchAndDelete() throws DateFormatException, CommandFormatException, StorageException, EventIdException, SearchException, EventDateException {
		Calendar from = Calendar.getInstance();
		from.add(Calendar.DAY_OF_YEAR, 1);
		Calendar to = from;
		loadTestData(from, to);
		Logic logic = new Logic();
		LogicAPI logicApi = new LogicAPI(logic);
		ArrayList<Event> result = logicApi.search(Language
				.getString("ADD_FLOATING_EVENT"));
		
		assertTrue(!result.isEmpty());

		try {
			result = logicApi.delete("all");
		} catch (StorageException | EventIdException e) {
			System.out.println(e.getMessage());
		}
		result = logicApi.search(Language.getString("ADD_FLOATING_EVENT"));

		assertTrue(result.isEmpty());
	}

	//@author A0098803W
	/**
	 * This method populates storage with data.
	 * 
	 * @param from
	 * @param to
	 */
	private void loadTestData(Calendar from, Calendar to) {
		List<Event> testList = new ArrayList<Event>();
		String timeStamp = " created on "
				+ Utilities.calendarToString(from,
						Language.getString("DATE_FORMAT"));
		testList.add(new FloatingEvent(Language.getString("ADD_FLOATING_EVENT")
				+ timeStamp));
		DeadlineEvent de = new DeadlineEvent(
				Language.getString("ADD_DEADLINE_EVENT") + timeStamp, to);
		de.setCompleted(true);
		testList.add(de);
		// testList.add(new TimedEvent(Language.getString("ADD_TIMED_EVENT")
		// + timeStamp, from, to));

		try {
			StorageAPI.storeEventList(testList);
		} catch (StorageException e) {
			assertTrue(false);
		}
	}

}
